package stark.app.function.view;

import android.graphics.Color;

import com.blankj.utilcode.util.SizeUtils;
import com.stark.customview.turntable.StkTextLineToCenterAdapter;
import com.stark.customview.turntable.StkTextOnArcAdapter;
import com.stark.customview.turntable.StkTurnTableItem;
import com.stark.customview.turntable.StkTurnTableView;

import java.util.ArrayList;
import java.util.List;

import stark.app.R;
import stark.app.databinding.FragmentStkTurnTableBinding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * Author：daizhongbin on 2023/9/8
 * Email：1027154338@qq.com
 * Desc:
 */
public class StkTurnTableFragment extends BaseNoModelFragment<FragmentStkTurnTableBinding> {

    private static final String[] textArray = new String[]{"招人干杯", "喝一杯", "男士喝", "真心话", "随意喝", "大冒险",
            "喝两杯", "找个人干杯", "上一家喝", "女士喝", "再转一次", "下一家喝"};
    private static final String[] textArray1 = new String[]{"数据1", "数据2", "数据3", "数据4", "数据5", "数据6", "数据7", "数据8"};
    private boolean useTextArray = true;

    @Override
    protected int onCreate() {
        return R.layout.fragment_stk_turn_table;
    }

    @Override
    protected void initView() {
        //设置监听器
        mDataBinding.turnTableContainer.setListener(new StkTurnTableView.IListener() {
            @Override
            public void onRotateStart() {
                //开始转动时的回调
            }

            @Override
            public void onRotateEnd() {
                //转动结束时的回调
            }

            @Override
            public void onSelItem(int pos) {
                //转动结束时，指针所指向的数据位置
                StkTurnTableItem item = (StkTurnTableItem) mDataBinding.turnTableContainer.getAdapter().getItem(pos);
                mDataBinding.tvRet.setText("指针指向结果：" + item.text);
            }
        });
        /**
         * 如果库内的适配器不满足要求，自己还可参照下面的两个适配器去继承StkTurnTableView.Adapter来实现满足自己需求的适配器
         */
        //数据适配器1
        StkTextLineToCenterAdapter adapter = new StkTextLineToCenterAdapter();
        adapter.setTextHOffset(-20);
        //数据适配器2
        //StkTextOnArcAdapter adapter = new StkTextOnArcAdapter();
        //adapter.setTextVOffset(-15);
        adapter.setDataList(loadData(textArray));
        mDataBinding.turnTableContainer.setAdapter(adapter);

        mDataBinding.btnStarRotate.setOnClickListener(v -> {
            //转动
            mDataBinding.turnTableContainer.startRotate();
        });
        mDataBinding.btnChangeData.setOnClickListener(v -> {
            useTextArray = !useTextArray;
            if (useTextArray) {
                adapter.setDataList(loadData(textArray));
            } else {
                adapter.setDataList(loadData(textArray1));
            }
        });
    }

    @Override
    protected void initData() {
    }

    private List<StkTurnTableItem> loadData(String[] textArray) {
        List<StkTurnTableItem> items = new ArrayList<>();
        StkTurnTableItem item;
        for (int i = 0; i < textArray.length; i++) {
            item = new StkTurnTableItem();
            item.text = textArray[i];
            item.bgColor = i % 2 == 0 ? Color.parseColor("#D2CCFB") : Color.WHITE;
            item.textColor = Color.parseColor("#6458C8");
            item.textSize = SizeUtils.sp2px(15);
            items.add(item);
        }
        return items;
    }

}
